#!/bin/bash
# -*- encoding: utf8 -*-
from BModel.md5Hive import md5Encode
from config.config import Hivefile, HiveHistoryfile, count, max_count
from config.config import sqllimit
import time


"""old库存储的数据 库时间节点（2017-09）"""
def OldDataInsertHiveNewThree(db):
    print 'sql查询'

    oldsql = "SELECT ReviewID,OrderID FROM `bw_ctrip_review_cn` WHERE WritingDate >='2017-10-01 00:00:00' AND WritingDate < '2017-11-01 00:00:00'"
    # fp = open(HiveHistoryfile, 'a+')
    # fp.write("查询区间2013-01-01 00:00:00"+ "\n")
    all = db.bmquery(oldsql, 'ctripo')
    reviewlist = []
    revirenum = len(reviewlist)

    fp = open(HiveHistoryfile, 'a+')
    # fp.write("查出旧表旧数据全部" + str(revirenum) + "条" + "\n")
    # print all['data'][0]['ReviewID']
    if len(all['data']) > 0:
        fp.write("查出旧表旧数据全部" + str(len(all['data'])) + "条" + "\n")
        page = int(len(all['data']) / sqllimit) + 1
        i = 0
        for i in range(0, page):
            print i
            start = i * sqllimit
            end = sqllimit * (i + 1)
            import time
            time = time.strftime("%Y-%m-%d")
            insertsql, stip = "", ""
            for v in all['data'][start:end]:
                insertsql = insertsql + stip + "(%s,'%s','%s')" % (v['ReviewID'], md5Encode(str(v['OrderID'])), time)
                stip = ","
            # print insertsql
            if insertsql:
                fp.write(insertsql + "\n")
                hotelsql = "INSERT INTO BW_Order_Review_List(`ReviewID`,`Order`,`DateTime`) VALUES %s " % (insertsql)
                db.bmquery(hotelsql, 'hive')
                print 'ok'


"""New库存储的数据 库时间节点（2017-09）"""
def NewDataInsertHiveThree(db):
    py_time = [1483200000000,1485878400000,1488297600000,1490976000000,1493568000000,1496246400000,1498838400000,1501516800000,1504195200000,1506787200000,
               1509465600000,1512057600000,1514736000000,1517414400000,1519833600000,1522512000000,1525104000000]
    for main_time in range(0,len(py_time)):
        if main_time+1 == len(py_time):
            date_end = int(time.time()) * 1000
        else:
            date_end = py_time[main_time + 1]

        newsql = "SELECT ID,ReviewID,OrderID FROM `bw_ctrip_review_cn_v2` WHERE WritingDate >= %s AND  WritingDate < %s"%(py_time[main_time],date_end)
        print newsql
        all = db.bmquery(newsql,'ctripn')
        maxid = 0
        revirenum = len(all['data'])
        print revirenum
        if 0 < revirenum:
            page = int(revirenum/sqllimit)+1
            i = 0
            add_time = time.strftime("%Y-%m-%d")
            for i in range(0,page):
                print i
                start = i*sqllimit
                end = sqllimit*(i+1)
                insertsql, stip = "", ""
                for v in all['data'][start:end]:
                    maxid = v['ID']
                    insertsql = insertsql+stip +"(%s,'% s','%s')"%(v['ReviewID'], md5Encode(str(v['OrderID'])), add_time)
                    stip = ","
                if insertsql:
                    hotelsql = "INSERT INTO BW_Order_Review_List(`ReviewID`,`Order`,`DateTime`) VALUES%s"%(insertsql)
                    db.bmquery(hotelsql,'hive')

        if 0 < maxid:
            fp = open(max_count,'w+')
            fp.write(str(maxid))
            fp.close()
